package com.bluegalaxy.nhb.snapi.core.config;

import com.bluegalaxy.nhb.snapi.core.common.OvervivwUtil;
import com.bluegalaxy.nhb.snservice.common.utils.StringUtil;
import com.bluegalaxy.nhb.snservice.core.exception.SignException;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * @description: To change this template use File | Settings | File Templates.
 */
public class XxInterceptor implements HandlerInterceptor {


    private Logger logger = LoggerFactory.getLogger(XxInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        PageHelper.clearPage();
        String str = request.getRequestURI();
//        logger.info("request ip:"+request.getRemoteHost()+"    uri: "+str );
        String uri = str.split("\\?")[0];
        long curLong = System.currentTimeMillis();
//        Object cacheValue = CacheUtils.get("xiaoxiangCache","expire_time_key");
        Long cacheValue = OvervivwUtil.get("expire_time_key");
        if (cacheValue == null){
            throw new SignException("value is empty");
        }
        if (curLong > cacheValue){
            throw new SignException("license expire");
        }

      /*  String signKey = request.getHeader("signKey");
        if (StringUtil.isEmpty(signKey)){
            throw new SignException("signKey 为空");
        }*/

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        logger.info("postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        logger.info("afterCompletion");
    }
}
